<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>名人故事提问</title>
    <link rel="stylesheet" href="styles.css">
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            display: flex;
            flex-direction: column;
            gap: 20px;
        }
        .case-sections {
            display: flex;
            gap: 20px;
        }
        .case-section {
            flex: 1;
            background-color: #f9f9f9;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            height: 200px;
            overflow-y: auto;
        }
        .question-section {
            background-color: white;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        textarea {
            width: 100%;
            height: 100px;
            margin-bottom: 10px;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            font-size: 16px;
            transition: all 0.3s ease;
            background: linear-gradient(145deg, #ffffff, #f9f9f9);
        }
        textarea:focus {
            border-color: #007bff;
            box-shadow: 0 0 10px rgba(0, 123, 255, 0.3);
            outline: none;
        }
        button {
            padding: 10px 20px;
            background: linear-gradient(145deg, #007bff, #0056b3);
            color: white;
            border: none;
            border-radius: 10px;
            cursor: pointer;
            font-size: 16px;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        button:hover {
            background: linear-gradient(145deg, #0056b3, #007bff);
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 123, 255, 0.3);
        }
        .response {
            margin-top: 20px;
            padding: 20px;
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            font-size: 16px;
            line-height: 1.6;
        }
        .case-section h3 {
            margin-top: 0;
        }
        .case-section ul {
            list-style-type: none;
            padding: 0;
        }
        .case-section li {
            margin-bottom: 10px;
            cursor: pointer;
            color: #007bff;
        }
        .case-section li:hover {
            text-decoration: underline;
        }
        .loading {
            text-align: center;
            font-size: 18px;
            color: #007bff;
        }
    </style>
</head>
<body>
    <div class="container">
        <!-- 上方案例展示 -->
        <div class="case-sections">
            <div class="case-section">
                <h3>按时间阶段选择</h3>
                <ul>
                    <li onclick="selectStage('古代')">古代名人</li>
                    <li onclick="selectStage('近代')">近代名人</li>
                    <li onclick="selectStage('现代')">现代名人</li>
                </ul>
            </div>
            <div class="case-section" id="famous-people">
                <h3>热门名人案例</h3>
                // 修改默认热门名人
                <ul id="famous-people-list">
                    <li onclick="selectCase('科技先锋')">科技先锋</li>
                    <li onclick="selectCase('商业巨擘')">商业巨擘</li>
                    <li onclick="selectCase('创新大师')">创新大师</li>
                </ul>
                
                // 修改阶段与名人映射
                const stageToPeople = {
                    '古代': ['文圣', '诗仙', '智圣'],
                    '近代': ['文豪', '革命先驱', '伟大领袖'],
                    '现代': ['科技先锋', '商业巨擘', '创新大师']
                };
            </div>
        </div>

        <!-- 下方提问区域 -->
        <div class="question-section">
            <h1>名人故事提问</h1>
            <textarea id="question" placeholder="请输入你想了解的名人..."></textarea>
            <button onclick="submitQuestion()">提交</button>
            <div id="response" class="response"></div>
        </div>
    </div>

    <script>
        // 阶段与名人映射
        const stageToPeople = {
            '古代': ['文圣', '诗仙', '智圣'],
            '近代': ['文豪', '革命先驱', '伟大领袖'],
            '现代': ['科技先锋', '商业巨擘', '创新大师']
        };

        // 选择阶段
        function selectStage(stage) {
            const peopleList = document.getElementById('famous-people-list');
            peopleList.innerHTML = ''; // 清空当前列表

            // 动态生成名人列表
            stageToPeople[stage].forEach(person => {
                const li = document.createElement('li');
                li.textContent = person;
                li.onclick = () => selectCase(person);
                li.style.cursor = 'pointer';
                li.style.color = '#007bff';
                li.style.marginBottom = '10px';
                li.addEventListener('mouseenter', () => li.style.textDecoration = 'underline');
                li.addEventListener('mouseleave', () => li.style.textDecoration = 'none');
                peopleList.appendChild(li);
            });
        }

        // 选择名人
        function selectCase(caseName) {
            document.getElementById('question').value = caseName;
            submitQuestion();
        }

        // 提交问题
        async function submitQuestion() {
            const question = document.getElementById('question').value;
            const responseDiv = document.getElementById('response');
            responseDiv.innerHTML = '<div class="loading">加载中...</div>';

            try {
                const response = await fetch('/ask', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ question }),
                });
                const data = await response.json();
                responseDiv.innerHTML = data.answer; // 直接显示答案
            } catch (error) {
                responseDiv.innerHTML = '请求失败，请重试。';
            }
        }
    </script>
    <script src="script.js"></script>
</body>
</html>